﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace TestForm
{
    class MSXML2DomWraper
    {
        private MSXML2.DOMDocumentClass xmldom;

        public string XmlText
        {
            get { return xmldom.xml; }
        }

        public MSXML2.DOMDocumentClass XmlDom
        {
            get { return xmldom; }
        }

        private MSXML2.IXMLDOMNode curNode = null;
        private MSXML2.IXMLDOMElement curElement = null;

        public MSXML2DomWraper(MSXML2.DOMDocumentClass xmldom)
        {
            this.xmldom = xmldom;
        }

        public MSXML2DomWraper(string xml)
        {
            this.xmldom = new MSXML2.DOMDocumentClass();
            xmldom.loadXML(xml);
        }

        public void SetCurDomNode(MSXML2.IXMLDOMNode node)
        {
            curNode = node;
        }

        public void SetCurDomElement(MSXML2.IXMLDOMElement node)
        {
            curElement = node;
        }

        public void SetCurDomNode()
        {
            MSXML2.IXMLDOMNode sendNode = selectSingleNode("//rs:data/z:row");
            curNode = sendNode;
        }

        public string GetNamedItemAttr(string name)
        {
            MSXML2.IXMLDOMNode node = null;
            string attrText = string.Empty;
            if (curNode == null) return string.Empty;

            node = curNode.attributes.getNamedItem(name);
            if (node != null)
            {
                attrText = node.nodeValue.ToString();
            }
            return attrText;
        }

        public void SetAttribute(string name, object nodeValue)
        {
            string attrText = string.Empty;
            if (curElement == null) return;

            curElement.setAttribute(name, nodeValue);
        }

        public MSXML2.IXMLDOMElement createElement(string tagName)
        {
            return xmldom.createElement(tagName);
        }

        public MSXML2.IXMLDOMNode selectSingleNode(string queryString)
        {
            return xmldom.selectSingleNode(queryString);
        }

        public MSXML2.IXMLDOMNodeList selectNodes(string queryString)
        {
            return xmldom.selectNodes(queryString);
        }
    }
}
